<?php


namespace Convert\Controller;

use Think\Controller;

class ApplicationController extends BaseController
{

    public function _initialize()
    {
        parent::_initialize();
    }


    public function convert()
    {
        $table_list = application_table();
        $this->assign('table_list', $table_list);

        $path = './Application/Convert/Data/application.json';
        $json = file_get_contents($path);
        $data= json_decode($json,true);

        $this->assign('data', $data);
        $this->display();
    }


    protected  function getNext($current)
    {
        $table_list = application_table();
        $next = get_next($table_list, $current);
        return $next['name'];
    }


    protected function getTableName($table){
        $table_list = application_table();
        $newTable = $table_list[$table];
        return $newTable['new_name']?$newTable['new_name']:$newTable['name'];
    }


    protected function convert_weibo($data){
        foreach($data as &$v){
            $v['data'] = str_replace('sourse','source',$v['data']);
            $v['content'] = stripslashes( $v['content']);
        }
        unset($v);
        return $data;
    }

    protected function convert_group_lzl_reply($data){
        foreach($data as &$v){
            $v['status'] = $v['is_del'] == 1 ? -1: $v['status'];
        }
        unset($v);
        return $data;
    }


    protected function convert_group_member($data){

        $group = array();
        foreach($data as &$v){
            if(in_array($v['group_id'],$group)){
                $v['position'] = 1;
            }else{
                $v['position'] = 3;
                $group[] = $v['group_id'];
            }

        }
        unset($v);
        return $data;
    }

    protected function convert_group_post($data){
        foreach($data as &$v){
            $v['summary'] =  mb_substr(text($v['content']), 0,  modC('SUMMARY_LENGTH', 50), 'utf-8');
            $cover = get_pic($v['content']);
            $v['cover'] = $cover == null ? '' : $cover;
        }
        unset($v);
        return $data;
    }


    protected function convert_event_attend($data){
        foreach($data as &$v){
            $v['create_time'] = $v['creat_time'] ;
        }
        unset($v);
        return $data;
    }


    protected function convert_shop($data){
        foreach($data as &$v){
            $v['money_need'] = $v['tox_money_need'] ;
        }
        unset($v);
        return $data;
    }


    protected function convert_document($data){

        truncate_table('news_detail');

        foreach($data as &$v){
            $art = $this->M('document_article')->where(array('id'=>$v['id']))->find();


            $v['description'] = msubstr(text($art['content']),0,200);
            $v['category'] = 1;
            $v['comment'] = 0;
            $v['cover'] =  $v['cover_id'] ;
            // 时间最大值
            $v['dead_line'] = intval(2147483647) ;

            $data_detail['news_id'] = $v['id'];
            $data_detail['content'] = $art['content'];
            M('news_detail')->add($data_detail);
        }
        unset($v);
        return $data;
    }

}